/**
 * Created by zhangyilin on 2018/12/9.
 */



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Doing")

public class Doing extends HttpServlet {

    private static String TaskContent = null;



    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库team
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }


    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String TaskTheme = req.getParameter("TaskTheme");
        System.out.println(TaskTheme);

        Connection conn = GetConnection();

        conn = GetConnection();
        PreparedStatement getTaskconPS = null;
        ResultSet getTaskconRS = null;//查询获取任务内容
        String getTaskconSql =  "select TaskContent from v_task_stu where TaskTheme=?";
        if(conn != null)
            try {
                getTaskconPS = conn.prepareStatement(getTaskconSql);
                if(getTaskconPS != null) {
                    getTaskconPS.setString(1, TaskTheme);
                    getTaskconRS = getTaskconPS.executeQuery();
                    if(getTaskconRS != null && getTaskconRS.next()) {
                        TaskContent = getTaskconRS.getString("TaskContent");

                    }

                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskconRS != null)
                        getTaskconRS.close();
                    if(getTaskconPS != null)
                        getTaskconPS.close();
                    if(conn != null)
                        conn.close();
                    getTaskconRS = null;
                    getTaskconPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }




        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String time = df.format(new Date());
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>输出</title>");
        out.println("</head>");
        out.println("<style>");
        out.println("h1{text-shadow: 5px 5px 5px #FF0000;}");
        out.println("</style>");
        out.println("<body background=\"背景.jpg\" style=\"background-repeat: no-repeat; background-size: 100% 100%; background-attachment: fixed;\">");
        out.println("<center>");
        out.println("<h3>查看结果</h3>");
        out.println("<br>");
        //out.println("任务内容:"+taskS.getTaskContent());
        out.println("<br>");
        out.println("现日期:"+time);
        out.println("<h3>该任务内容</h3>");


        out.println("<br>");
        out.println("<center>");
        out.println("内容："+TaskContent);
        out.println("</center>");
        out.println("</body>");
        out.println("</html>");




    }



    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost( req,resp);
    }

}
